/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pa165.pneu.pneuservisbackend.services;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pa165.pneu.pneuservisbackend.dao.TyreTypeDAO;
import pa165.pneu.pneuservisbackend.entiies.TyreType;
import pa165.pneu.pneuservisbackend.to.TyreTypeTO;

/**
 *
 * @author Lukas Stevko
 */
@Service
public class TyreTypeServiceImpl implements TyreTypeService {
    
    @Autowired
    private TyreTypeDAO tyreTypeDAO;
    
    @Transactional
    @Override
    public void createTyreType(TyreTypeTO tyreType) {
        TyreType tyre = tyreType.toTyreType();
        tyreTypeDAO.createTyreType(tyre);
        tyreType.setId(tyre.getId());
    }
    
    @Transactional
    @Override
    public TyreTypeTO getTyreType(Long id) {
        TyreTypeTO result;
        result = new TyreTypeTO(tyreTypeDAO.getTyreType(id));
        return result;
    }
    
    @Transactional
    @Override
    public void updateTyreType(TyreTypeTO tyreTypeDao) {
        tyreTypeDAO.updateTyreType(tyreTypeDao.toTyreType());
    }
    
    @Transactional
    @Override
    public void removeTyreType(TyreTypeTO tyreTypeDao) {
        tyreTypeDAO.removeTyreType(tyreTypeDao.toTyreType());
        tyreTypeDao.setId(null);
    }
    
    @Transactional
    @Override
    public List<TyreTypeTO> getAllTyreTypes() {
        List<TyreType> tyreTypeList;
        tyreTypeList = tyreTypeDAO.getAllTyreTypes();
        List<TyreTypeTO> result = new ArrayList<>();
        for (TyreType t : tyreTypeList) {
            result.add(new TyreTypeTO(t));
        }
        return result;
    }
    
    @Transactional
    @Override
    public List<TyreTypeTO> findTyreTypesByRimSize(int rimSize) {
        List<TyreType> tyreTypeList;
        tyreTypeList = tyreTypeDAO.findTyresByRimSize(rimSize);
        List<TyreTypeTO> result = new ArrayList<>();
        for (TyreType t : tyreTypeList) {
            result.add(new TyreTypeTO(t));
        }
        return result;
    }
    
    @Transactional
    @Override
    public List<TyreTypeTO> findTyreTypesByWidth(int width) {
        List<TyreType> tyreTypeList;
        tyreTypeList = tyreTypeDAO.findTyresByWidth(width);
        List<TyreTypeTO> result = new ArrayList<>();
        for (TyreType t : tyreTypeList) {
            result.add(new TyreTypeTO(t));
        }
        return result;
    }
    
    @Transactional
    @Override
    public List<TyreTypeTO> findTyreTypesByProfile(int profile) {
        List<TyreType> tyreTypeList;
        tyreTypeList = tyreTypeDAO.findTyresByProfile(profile);
        List<TyreTypeTO> result = new ArrayList<>();
        for (TyreType t : tyreTypeList) {
            result.add(new TyreTypeTO(t));
        }
        return result;
    }
    
    @Transactional
    @Override
    public List<TyreTypeTO> findTyreTypesByPrice(BigDecimal price) {
        List<TyreType> tyreTypeList;
        tyreTypeList = tyreTypeDAO.findTyresByPrice(price);
        List<TyreTypeTO> result = new ArrayList<>();
        for (TyreType t : tyreTypeList) {
            result.add(new TyreTypeTO(t));
        }
        return result;
    }
    
    @Transactional
    @Override
    public List<TyreTypeTO> findTyreTypesByManufacturer(String manufacturer) {
        List<TyreType> tyreTypeList;
        tyreTypeList = tyreTypeDAO.findTyresByManufacturer(manufacturer);
        List<TyreTypeTO> result = new ArrayList<>();
        for (TyreType t : tyreTypeList) {
            result.add(new TyreTypeTO(t));
        }
        return result;
    }
}